package com.example.demo.controller;

import com.example.demo.result.Result;
import com.example.demo.service.MessageService;
import com.example.demo.vo.messageDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chat")
@Tag(name = "普通ai会话接口")
public class MessageController {



    final MessageService messageService;
    /*
    普通的对话助手
     */
//    @Operation(summary = "ai对话,用户发送消息接口")
//    @GetMapping("/common")
//    public Result<messageDetailVO> highChat(@RequestParam(value = "userId")Long userId,
//                           @RequestParam(value = "message")String message,
//                           @RequestParam(required = false) Long sessionId){
//        return Result.success(messageService.response(userId,message,sessionId));
//    }


    /**
     * 获取某个会话里的信息
     * @param userId
     * @param sessionId
     * @return
     */
    @Operation(summary = "获取某个会话内所有信息接口")
    @GetMapping("/detail")
    public Result<List<messageDetailVO>> getMessageDetail(@RequestParam(value = "userId")Long userId,
                                                          @RequestParam(value = "sessionId")Long sessionId){
        List<messageDetailVO> messageDetailVOList=messageService.getMessageDetail(userId,sessionId);
        return Result.success(messageDetailVOList);
    }

    /**
     * 流式传输对话中的信息
     * @param message
     * @param sessionId
     * @return
     */
    @Operation(summary = "ai对话流式回复接口")
    @GetMapping(value = "/stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<Object>> streamChat(@RequestParam(value = "message")String message,
                                                    @RequestParam(value = "sessionId") Long sessionId,
                                                    @RequestParam(value = "webSearch")Boolean webSearch){
        String personal="你是一个ai智能助手，你能能够回答用户的问题。";
        return messageService.streamResponse(message,sessionId,webSearch,personal);
    }

}
